﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) www.51mvc.com Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.51mvc.com/
 * Create By 小鱼(Leven)  - http://leven.com.cn
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using Infancy.Core.Entities;
using Infancy.Core.Repository;

namespace Infancy.Data.Aef {
    /// <summary>
    /// 对Aef的基本操作进行封装,数据操作实现直接或者间接继承该类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public abstract class AefRepositoryBase<TEntity,TAefEntity> : RepositoryBase<TEntity, ObjectContext>
        where TEntity : IEntity{
        //where TAefEntity : System.Data.Objects.DataClasses.EntityObject{
        internal AefRepositoryBase(RepositoryContext context)
            : base(context) {
        }

        private ObjectQuery<TAefEntity> query;
        private string name;

        protected string Name {
            get {
                if (name == null) {
                    //暂时用这行代替
                    name = String.Format("[{0}]", typeof(TEntity).Name);
                }
                return name;
            }
        }

        protected ObjectQuery<TAefEntity> Query {
            get {
                if (query == null) {
                    query = Context.Instance.CreateQuery<TAefEntity>(Name);
                }
                return query;
            }
        }

        #region simple GUID methods

        public override void Insert(TEntity entity) {
            Context.Instance.AddObject(Name, entity);
            Context.Instance.SaveChanges();
        }

        public override void Update(TEntity entity) {
            Context.Instance.SaveChanges();
        }

        public override void Delete(TEntity entity) {
            Context.Instance.DeleteObject(entity);
            Context.Instance.SaveChanges();
        }

        public override void Delete(long id) {
            TEntity entity = GetSingle(id);
            Delete(entity);
        }

        public override int Count() {
            return Context.Instance.CreateQuery<int>(
                String.Format("select value count(e.id) from {0} as e", Name))
                .First();
        }

        public override TEntity GetSingle(long id) {
            throw new NotImplementedException();
        }

        public override IList<TEntity> GetList() {
            throw new NotImplementedException();
        }

        public override TEntity Create() {
            throw new NotImplementedException();
        }

        #endregion
    }
}
